package com.lp.controller.iot;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lp.bean.IotVideoInfo;
import com.lp.bo.IotVideoInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.Code;
import com.lp.common.CodeIot;
import com.lp.common.Constants;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.ezuiz.EzuizService;
import com.lp.service.IotVideoInfoService;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;

@Controller

public class IotVideoInfoController extends BaseController {
	@Autowired
	private EzuizService ezuizService;
	
	@Autowired
	private IotVideoInfoService iotVideoInfoService;
	/**
	 * 检索
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.IotVideoInfo.IOT_VIDEO_INFO_PAGE)
	public ModelAndView selectPage(HttpServletResponse response,
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
			@RequestBody IotVideoInfoBO obj, @RequestParam(required = false) Integer pageSize,
			@RequestParam Integer paged) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			resultMap = service.selectPageList("IotVideoInfo.selectPage", getPageBean(paged, pageSize), obj);
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 插入
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.IotVideoInfo.IOT_VIDEO_INFO)
	public ModelAndView save(HttpServletResponse response,
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
			@RequestBody IotVideoInfoBO obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			obj.setStatus(CodeIot.DEVICE_STATUS.UNCONTECT);
			if(ObjectUtil.isEmpty(ProCacheUtil.getCache(CacheName.VIDEO_INFO, obj.getDevice_serial() ))){
				//添加萤石
				resultMap = ezuizService.addDevice(obj);
				if(isOk(resultMap)){
					resultMap = service.insert("IotVideoInfo.insert", obj);
					if( isOk(resultMap) ){
						ProCacheUtil.addCache(CacheName.VIDEO_INFO, obj.getDevice_serial(), obj);
					}
				}
			}else{
				putStatusCode(resultMap, Code.ResponseCode.IotInfo.VIDEO_INFO_REPEAT);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 查询单个
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.IotVideoInfo.IOT_VIDEO_INFO)
	public ModelAndView selectOne(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestParam Integer id) {
		Map<String, Object> resultMap = getResultMap();
		try {
			resultMap = service.selectOne("IotVideoInfo.selectOne", new IotVideoInfoBO(id));
		} catch (Exception e) {
			exception(e, resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 更新
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.IotVideoInfo.IOT_VIDEO_INFO)
	public ModelAndView update(HttpServletResponse response,
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfoBO obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if( ObjectUtil.isNotEmpty(obj.getId()) ){
				resultMap = service.update("IotVideoInfo.update", obj);
			}else{
				putStatusCode(resultMap, Code.ResponseCode.IotInfo.VIDEO_INFO_REPEAT);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 删除
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.IotVideoInfo.IOT_VIDEO_INFO)
	public ModelAndView delete(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestParam Integer id) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotVideoInfoBO obj = new IotVideoInfoBO();
			if (ObjectUtil.isEmpty(id)) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			} else {
				obj.setId(id);
				obj.setDelete_flag(Constants.DELETE.YES);
				resultMap = iotVideoInfoService.deleteVideo(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	
	/**
	 * 开始云台控制
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.Ptz.START)
	public ModelAndView ptzStart(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfoBO obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if(ObjectUtil.hasNull(obj.getId(),obj.getDirection(),obj.getSpeed())) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else {
				resultMap = ezuizService.ptzStart(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 停止云台控制
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.Ptz.STOP)
	public ModelAndView ptzStop(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfoBO obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if(ObjectUtil.hasNull(obj.getId())) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else {
				resultMap = ezuizService.ptzStop(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 同步摄像头
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.Ezuiz.SYNCHRONIZ)
	public ModelAndView synchronizEzuiz(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfo obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if(ObjectUtil.hasNull(obj.getScene_id())) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else {
				resultMap = ezuizService.synchronizDevice(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 开启设备视频加密
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.Ezuiz.ENCRYPTON)
	public ModelAndView encryptOn(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfo obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if(ObjectUtil.hasNull(obj.getId())) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else {
				resultMap = ezuizService.encryptOn(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 开启设备视频加密
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.Ezuiz.ENCRYPTOFF)
	public ModelAndView encryptOff(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfo obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if(ObjectUtil.hasNull(obj.getId())) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else {
				resultMap = ezuizService.encryptOff(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 抓取设备图片
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.Ezuiz.DEVICECAPTURE)
	public ModelAndView deviceCapture(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,@RequestBody IotVideoInfo obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			if(ObjectUtil.hasNull(obj.getId())) {
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else {
				resultMap = ezuizService.deviceCapture(obj);
			}
		} catch (Exception e) {
			exception(e, resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
}
